home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10007 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.0 KB

  1. Path: news.bridge.net!news
  2. From: psycho@bridge.net (Gary Thompson)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Crash Proofing?
  5. Date: Fri, 15 Mar 1996 07:45:06 GMT
  6. Organization: A poorly-installed InterNetNews site
  7. Message-ID: <4ib79j$6gv@news.bridge.net>
  8. References: <4hsfje$rbr@uwm.edu>
  9. NNTP-Posting-Host: ppp-ftl1-28.bridge.net
  10. X-Newsreader: Forte Free Agent 1.0.81
  11.  
  12. peterk@alpha2.csd.uwm.edu (Peter J Kleczka) wrote:
  13. >Hi all
  14. >    I'm new to C programing and am trying to re-write a program
  15. >I did in pascal in C.  When I wrote the pascal program I got
  16. >caught up in the details of getting the program to work and
  17. >consequentially readability of the code and user-friendlyness
  18. >suffered......
  19. >    The program I'm working on (below) works fine as long
  20. >as the user enters an integer.....but it gets caught in an
  21. >endless loop between the functions: firstmenu() and firstchoice()
  22. >when I enter, say, 5.5 instead of an integer
  23. >it's unlikely that the user would enter anything but an integer
  24. >when presented with integer choices...but I want to make the
  25. >code uncrashable .....what can I do if anything to make it
  26. >so that the program doesnt do wierd things if the user enters
  27. >the wrong thing here?
  28.  
  29. >/* prototype for the C version of block schedule program */
  30.  
  31. > blah-blah-blah
  32.  
  33. Call it a fault of scanf.  I never use it.  scanf REQUIRES the user to input
  34. ONLY the type requested.
  35.  
  36. >firstchoice()
  37. >{
  38. >    unsigned choice;
  39. >    scanf ("%d", &choice);
  40. >    while (choice !=1 && choice != 2 && choice != 3 && choice !=4)
  41. >    {
  42. >        printf("You entered %d ... please choose a number between 1-4\n\n", choice);
  43. >        firstmenu(); /* remind user of appropos choices */
  44. >        scanf("%d", &choice);
  45. >    }
  46. >}
  47.  
  48. Change it to this...
  49.  
  50. firstchoice()
  51. {
  52.     unsigned choice=0;
  53.     char buff[10];
  54.  
  55.     do
  56.     {
  57.         firstmenu();  /* take firstmenu out of the MAIN function */
  58.         gets(buff);
  59.         choice=atoi(buff);
  60.     }
  61.     while(choice<1 || choice>4)
  62.         
  63. }
  64. That's a little different.  A bit shorter and will save on code space.  strictly
  65. up to you tho... there are thousands of ways of doing it.
  66.  
  67. Only problem with this is if the user enters a false-entry of more than 10 chars
  68. you will get a variable overload which may crash the program.  You can fix that
  69. by replacing...
  70.  
  71. gets(buff)
  72. with...
  73. fgets(stdin,buff,5);
  74.  
  75. ( may have the order and name of the standard input constant wrong...) This is
  76. cheating but it works just fine.  You are forcing the user to input only 5
  77. chars.  I think it will allow you to input more but only 5 will be taken.  This
  78. prevents an overload.  The only way to only allow 5 chars to be input is to
  79. design your own input routine.  That's not an easy task but the only way to make
  80. it 100% bullet-proof.  (That's what I always do...  it does make for extremely
  81. versatile programs...)
  82.  
  83.  
  84.                               Gary Thompson
  85.                                "The Psycho"
  86.                              psycho@bridge.net
  87.                         72607.1365@compuserve.com
  88.                        http://www.bridge.net/~psycho
  89.            HTTP://ourworld.compuserve.com/homepages/psychotps
  90.  
  91.